package com.bjqh.edu.UI.Activity.StudentPersonal;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.OrderDetail;
import com.bjqh.edu.Bean.Orderdetails;
import com.bjqh.edu.R;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class PendingDetailActivity extends AppCompatActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_qx)
    TextView tvQx;
    @BindView(R.id.rl_top_tilte)
    RelativeLayout rlTopTilte;
    @BindView(R.id.iv_bg)
    ImageView ivBg;
    @BindView(R.id.tv_des1)
    TextView tvDes1;
    @BindView(R.id.tv_des2)
    TextView tvDes2;
    @BindView(R.id.bc)
    RelativeLayout bc;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_pp)
    TextView tvPp;
    @BindView(R.id.btn_payment)
    Button btnPayment;
    @BindView(R.id.main)
    LinearLayout main;
    Orderdetails.DataBean dataBean;
    OrderDetail.OrderBean orderBean;
    OrderDetail.OrderBean.CourseBean courseBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_pending_detail);
        ButterKnife.bind(this);
        tvTitle.setText("已取消");
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        Bundle bundle = (Bundle) getIntent().getExtras().get("bundle");
        dataBean = (Orderdetails.DataBean) bundle.getParcelable("data");
        OkGo.post(AppConstants.BASE_URL + "/order/detail.shtml")
                .tag(this)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .params("id", dataBean.getId())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.e("pending", s);
                        Gson gson = new Gson();
                        OrderDetail orderDetail = gson.fromJson(s, OrderDetail.class);
                        if (orderDetail.getMsg() != null) {
                            if (orderDetail.getMsg().equals("timeout")) {
                                AppConstants.gotoLogin(PendingDetailActivity.this);
                            } else if (orderDetail.getMsg().equals("remote login")) {
                                AppConstants.remologin(PendingDetailActivity.this);
                            }
                            else  if(orderDetail.getMsg().equals("ac none"))
                            {
                                btnPayment.setText("课程已售完");
                                btnPayment.setBackgroundColor(getResources().getColor(R.color.black_gray));
                                btnPayment.setEnabled(false);
                                orderBean = orderDetail.getOrder();
                                courseBean = orderBean.getCourse();
                                Glide.with(getApplication())
                                        .load(AppConstants.IMG_URL + courseBean.getImg())
                                        .error(R.mipmap.kcgm_bj)
                                        .into(ivBg);
                                String[] strarray = orderBean.getCoursedes().split(",");
                                tvDes1.setText(strarray[0]);
                                if (strarray.length > 1)
                                    tvDes2.setText("(" + strarray[1] + ")");
                                tvName.setText(dataBean.getCoursename());
                                tvPp.setText("￥" + String.valueOf(dataBean.getPrice()));

                            }
                            else if(orderDetail.getMsg().equals("ac end"))
                            {
                                btnPayment.setText("活动已结束");
                                btnPayment.setBackgroundColor(getResources().getColor(R.color.black_gray));
                                btnPayment.setEnabled(false);
                                orderBean = orderDetail.getOrder();
                                courseBean = orderBean.getCourse();
                                Glide.with(getApplication())
                                        .load(AppConstants.IMG_URL + courseBean.getImg())
                                        .error(R.mipmap.kcgm_bj)
                                        .into(ivBg);
                                String[] strarray = orderBean.getCoursedes().split(",");
                                tvDes1.setText(strarray[0]);
                                if (strarray.length > 1)
                                    tvDes2.setText("(" + strarray[1] + ")");
                                tvName.setText(dataBean.getCoursename());
                                tvPp.setText("￥" + String.valueOf(dataBean.getPrice()));

                            }
                            else {
                                orderBean = orderDetail.getOrder();
                                courseBean = orderBean.getCourse();
                                Glide.with(getApplication())
                                        .load(AppConstants.IMG_URL + courseBean.getImg())
                                        .error(R.mipmap.kcgm_bj)
                                        .into(ivBg);
                                String[] strarray = orderBean.getCoursedes().split(",");
                                tvDes1.setText(strarray[0]);
                                if (strarray.length > 1)
                                    tvDes2.setText("(" + strarray[1] + ")");
                                tvName.setText(dataBean.getCoursename());
                                tvPp.setText("￥" + String.valueOf(dataBean.getPrice()));
                            }
                        }



                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {

                    }
                });
        btnPayment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("ordernum",orderBean.getOrdernum());
                intent.putExtra("price",String.valueOf(orderBean.getPrice()));
                intent.putExtra("coursedes", orderBean.getCoursedes());
                intent.putExtra("coursename", orderBean.getCoursename());
                intent.putExtra("cid", String.valueOf(orderBean.getCid()));
                intent.setClass(PendingDetailActivity.this, PaymentActivity.class);
                PendingDetailActivity.this.startActivity(intent);
            }
        });
    }
}
